/**
 * 2018/06/26 create 张英辉
 * 加载3D效果文件
 */
var WEBUILDING3D = new function(){

    var _option = null;
    var _MatiralType = null;

    this.DefaultMaterial = "default/";
    this.NormalMaterial = "normal/";
    //初始化
    this.initalLoading = function(option){
        if(option == null || option == undefined){
            console.log("error:请传入option参数");
            return;
        }
        _option = option;
        loadJson(_option,_MatiralType);
    }  
    this.setMaterial = function(MatiralType){
        if(MatiralType == null || MatiralType == undefined){
            console.log("error:请传入MatiralType参数");
            return;
        }
        _MatiralType = MatiralType;
        loadJson(_option,_MatiralType);
    }  
}
function loadJson(option,MatiralType){
    var urlHead = "static/mtl/";
    var urlMaterial = WEBUILDING3D.DefaultMaterial ;
    if(MatiralType != undefined){
        urlMaterial = MatiralType;
    }
    $.ajaxSettings.async=false;
    for (var i=0;i<option.name.length;i++){

        var url= urlHead + urlMaterial + option.name[i] + ".json";
        $.getJSON(url,function (data,status) {
            if (status=="success"){
                var loader = new THREE.ObjectLoader();
                var loadedMesh = loader.parse(data);
                option.material[i]=loadedMesh.material;

                if(option.name[i]!="selecting"){  //selecting为拾取楼层时才有值
                    if(option.object[i] instanceof THREE.Group){
                        option.object[i].traverse(function (child) {
                            child.material=option.material[i];
                        });
                        // console.log("成功导入 "+option.name[i]+"材质！");
                    }else if(option.object[i] instanceof THREE.Mesh){
                        option.object[i].material=option.material[i];
                        // console.log("成功导入 "+option.name[i]+"材质！");
                    }else {
                        // console.log("----------无法识别该对象----------")
                    }
                }else{
                    // console.log("成功导入 "+option.name[i]+"材质！");
                }
                
                option.material[i].needsUpdate = true;

            }else {
                // console.log("----------初始化加载材质文件失败----------")
                // console.log(status);
            }
        });
    }

    //光照
    for (var i=0;i<option.light.length;i++){
        if (option.light[i] instanceof THREE.AmbientLight){
            var url= urlHead + urlMaterial + "AmbientLight.json";
            $.getJSON(url,function (data,status) {
                if (status=="success"){
                    option.light[i].color.set(data.AmbientLightColor);
                    option.light[i].visible=data.visible;
                    option.light[i].intensity=data.intensity;
                    // console.log("成功加载AmbientLight效果!");
                }else {
                    // console.log("----------初始化加载AmbientLight文件失败----------")
                    // console.log(status);
                }
            });
        }else if (option.light[i] instanceof THREE.PointLight){
            var url= urlHead + urlMaterial + "PointLight.json";
            $.getJSON(url,function (data,status) {
                if (status=="success"){
                    option.light[i].color.set(data.color);
                    option.light[i].intensity=data.intensity;
                    option.light[i].visible=data.visable;
                    option.light[i].position.x=data.positionX;
                    option.light[i].position.y=data.positionY;
                    option.light[i].position.z=data.positionZ;
                    // console.log("成功加载PointLight效果!");

                }else {
                    console.log(status);
                }
            });
        }else if (option.light[i] instanceof THREE.SpotLight){
            var url= urlHead + urlMaterial + "SpotLight.json";
            $.getJSON(url,function (data,status) {
                if (status=="success"){
                    option.light[i].color.set(data.SpotLightColor);
                    option.light[i].intensity=data.intensity;
                    option.light[i].visible=data.SLVisable==undefined?true:data.SLVisable;
                    option.light[i].position.x=data.SLPositionX;
                    option.light[i].position.y=data.SLPositionY;
                    option.light[i].position.z=data.SLPositionZ;
                    // console.log("成功加载SpotLight效果!");

                }else {
                    console.log(status);
                }
            });
        }else if (option.light[i] instanceof THREE.DirectionalLight){
            var url= urlHead + urlMaterial + "DirectionalLight.json";
            $.getJSON(url,function (data,status) {
                if (status=="success"){
                    option.light[i].color.set(data.DLightColor);
                    option.light[i].intensity=data.intensity;
                    option.light[i].visible=data.DLVisible;
                    option.light[i].position.x=data.DLPositionX;
                    option.light[i].position.y=data.DLPositionY;
                    option.light[i].position.z=data.DLPositionZ;
                    // console.log("成功加载DirectionalLight效果!");
                }else {
                    console.log(status);
                }
            });
        }else if (option.light[i] instanceof THREE.HemisphereLight){
            var url= urlHead + urlMaterial +"HemisphereLight.json";
            $.getJSON(url,function (data,status) {
                if (status=="success"){
                    option.light[i].skyColor.set(data.color);
                    option.light[i].groundColor.set(data.groundColor);
                    option.light[i].visible=data.visible;
                    option.light[i].intensity=data.intensity;
                    // console.log("成功加载HemisphereLight效果!");
                }else {
                    // console.log("----------初始化加载HemisphereLight文件失败----------")
                    // console.log(status);
                }
            });
        }
    }

    var sceneUrl= urlHead + urlMaterial +"sceneFog.json";
    $.getJSON(sceneUrl,function (data,status) {
        if (status=="success"){
            if (data.fogEnable){
                option.scene.fog=new THREE.Fog(data.fogColor,data.fogNear,data.fogFar);
            }else {
                option.scene.fog=null;
            }
            // console.log("成功加载雾化效果!");
        }else {
            console.log(status);
        }
    });

    var rendererUrl= urlHead + urlMaterial +"rendererColor.json";
    $.getJSON(rendererUrl,function (data,status) {
        if (status=="success"){
            option.renderer.setClearColor(data.color);
            // console.log("成功加载背景颜色!");

        }else {
            console.log(status);
        }
    });

    $.ajaxSettings.async=true;
    return option;
}